package org.lmw.note;

import java.util.ArrayList;
import java.util.List;

import org.lmw.note.adapter.NoteAdapter;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ViewSwitcher;

import com.google.gson.Gson;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;
import com.norbsoft.typefacehelper.TypefaceHelper;
import com.umeng.update.UmengUpdateAgent;


public class Act_Main extends FragmentActivity implements OnItemClickListener {

	ListView lv;
	List<Note> list;
	NoteAdapter adapter;
	ViewSwitcher switcher;
	ImageButton add_btn;
	EditText word;
	static final int REQ_CODE_EDIT=99;
	List<Note> search_res;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_main);
		UmengUpdateAgent.update(this);
		initView();
		
		TypefaceHelper.typeface(this);
	}
	
	private void initView(){
		switcher=(ViewSwitcher)findViewById(R.id.viewSwitcher1);
		word=(EditText)findViewById(R.id.word);
		word.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				search(word.getText().toString().trim());
			}
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
			@Override
			public void afterTextChanged(Editable arg0) {}
		});
		
		list=new ArrayList<Note>();
		lv=(ListView)findViewById(R.id.listview);
		adapter=new NoteAdapter(this,list);
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(this);
		
		notifyData();
		
		add_btn=(ImageButton)findViewById(R.id.add_btn);
		add_btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				startActivityForResult(new Intent(getApplicationContext(), Act_Edit.class), REQ_CODE_EDIT);
			}
		});
	}
	
	//刷新数据和列表 
	private  void notifyData(){
		try {
			list.clear();
			List<Note> temp=App.db.findAll(Selector.from(Note.class).where("status", "=", 0).orderBy("createTime",true));
			list.addAll(temp);
		} catch (DbException e) {
			e.printStackTrace();
		}
		adapter.notifyDataSetChanged();
	}
	
	//搜索框切换
	public void switcher(View v){
		if(v.getId()==R.id.action_search){
			switcher.setDisplayedChild(1);
		}
		if(v.getId()==R.id.action_cancel){
			switcher.setDisplayedChild(0);
			word.setText("");
			notifyData();
		}
	}
	
	//搜索
	private void search(String word){
		try {
			list.clear();
			List<Note> temp=App.db.findAll(Selector.from(Note.class).where("text", "like", "%"+word+"%").and("status", "=", 0).orderBy("createTime",true));
			list.addAll(temp);
		} catch (DbException e) {
			e.printStackTrace();
		}
		adapter.notifyDataSetChanged();
	}  
	
	//MENU 菜单
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
//		if(item.getItemId()==R.id.action_log){
//			startActivity(new Intent(getApplicationContext(), Act_Log.class));
//		}
		if(item.getItemId()==R.id.action_about){
			startActivity(new Intent(getApplicationContext(), Act_About.class));
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent it=new Intent(Act_Main.this,Act_Edit.class);
		it.putExtra("objStr",new Gson().toJson(list.get(arg2)));
		startActivityForResult(it,REQ_CODE_EDIT);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		notifyData();
	}
}
